package com.wen.yygh.user.api;

import com.wen.yygh.common.result.Result;
import com.wen.yygh.common.util.AuthContextHolder;
import com.wen.yygh.model.user.Patient;
import com.wen.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient/")
@Api(tags = "就诊人管理")
@Slf4j
public class PatientApiController {
    @Autowired
    private PatientService patientService;

    //根据id获取就诊人信息
    @ApiOperation("根据id获取就诊人信息")
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        log.info("userId=>" + userId);
        List<Patient> result = patientService.findAllByUserId(userId);
        return Result.ok(result);

    }


    @PostMapping("auth/save")
    @ApiOperation("添加就诊人")
    public Result addPatient(@RequestBody Patient patient, HttpServletRequest request) {
        //获取当前登录的用户id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);

        patientService.save(patient);
        return Result.ok();
    }

    @ApiOperation("根据id获取就诊人信息")
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable("id") Long id) {
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }

    @ApiOperation("修改就诊人")
    @PostMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return Result.ok();

    }

    @ApiOperation("根据id获取就诊人信息")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id) {
        Patient patient = patientService.getPatientById(id);
        return patient;
    }

    @ApiOperation("删除就诊人")
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable("id") Long id) {
        patientService.removeById(id);
        return Result.ok();
    }

}
